package com.crosspaste.ui.devices

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.contentColorFor
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import com.crosspaste.platform.Platform
import com.crosspaste.ui.LocalAppSizeValueState

interface PlatformScope {

    val platform: Platform

    val platformBackground: Color
        @Composable @ReadOnlyComposable
        get() = MaterialTheme.colorScheme.surfaceContainerHighest

    val selectedPlatformBackground: Color
        @Composable @ReadOnlyComposable
        get() = MaterialTheme.colorScheme.secondaryContainer

    fun getDeviceDisplayName(): String

    @Composable
    fun HoverableDeviceBarView(content: @Composable (Color) -> Unit) {
        val appSizeValue = LocalAppSizeValueState.current

        var hover by remember { mutableStateOf(false) }

        val background =
            if (hover) {
                selectedPlatformBackground
            } else {
                platformBackground
            }

        val onBackground = MaterialTheme.colorScheme.contentColorFor(background)

        Row(
            modifier =
                Modifier
                    .fillMaxWidth()
                    .height(appSizeValue.deviceHeight)
                    .background(background)
                    .then(
                        hoverModifier(
                            modifier = Modifier,
                            onHover = {
                                hover = true
                            },
                            onExitHover = {
                                hover = false
                            },
                        ),
                    ),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically,
        ) {
            DeviceBaseInfoView(
                platform = platform,
                deviceDisplayName = getDeviceDisplayName(),
                onBackground = onBackground,
            )

            Row(
                modifier = Modifier.wrapContentWidth(),
                horizontalArrangement = Arrangement.End,
                verticalAlignment = Alignment.CenterVertically,
            ) {
                content(background)
            }
        }
    }

    @Composable
    fun StaticDeviceBarView(content: @Composable (Color) -> Unit) {
        val appSizeValue = LocalAppSizeValueState.current
        Row(
            modifier =
                Modifier
                    .fillMaxWidth()
                    .height(appSizeValue.deviceHeight)
                    .background(platformBackground),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically,
        ) {
            DeviceBaseInfoView(
                platform = platform,
                deviceDisplayName = getDeviceDisplayName(),
                onBackground = MaterialTheme.colorScheme.contentColorFor(platformBackground),
            )

            Row(
                modifier = Modifier.wrapContentWidth(),
                horizontalArrangement = Arrangement.End,
                verticalAlignment = Alignment.CenterVertically,
            ) {
                content(platformBackground)
            }
        }
    }

    @Composable
    fun hoverModifier(
        modifier: Modifier,
        onHover: () -> Unit = {},
        onExitHover: () -> Unit = {},
    ): Modifier = modifier
}
